package com.kexinxin.util.search;

import java.io.IOException;
import java.util.List;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import com.kexinxin.bean.Question;
import com.kexinxin.bean.Skill;
import com.kexinxin.service.impl.SearchDAOImpl;


public class StoreFileUtils {

	private static Directory skillDirectory = null;
	private static Directory subjectDirectory = null;

	public static Directory getSkillDirectory() throws Exception {
		if (skillDirectory == null) {
			skillDirectory = new RAMDirectory();
			// skillDirectory = FSDirectory.open(new File("D://index2/test"));
			setUpWordDirectory();
		}
		return skillDirectory;
	}

	public static Directory getSubjectDirectory() throws Exception {
		if (subjectDirectory == null) {
			subjectDirectory = new RAMDirectory();
			// skillDirectory = FSDirectory.open(new File("D://index2/test"));
			setUpSubjectDirectory();
		}
		return subjectDirectory;
	}

	public static Directory getSubjectDirectoryBySearchConditon(SearchCondition searchCondition) throws IOException {
		Directory subjectDirectoryBySearchCondition = new RAMDirectory();

		IndexWriter writer = new IndexWriter(subjectDirectoryBySearchCondition,
				new SmartChineseAnalyzer(Version.LUCENE_34), // 2
				IndexWriter.MaxFieldLength.UNLIMITED);
		List<Question> subjectList = SearchDAOImpl.getSubjectList();
		for (int i = 0; i < subjectList.size(); i++) { // 3
			Document doc = new Document();

			if (searchCondition.type == 0) {
				if (searchCondition.difficuty[0] < subjectList.get(i).getDifficulty()
						&& searchCondition.difficuty[1] > subjectList.get(i).getDifficulty()
						&& searchCondition.skillList.contains(subjectList.get(i).getSkillName())) {
					doc.add(new Field("content", subjectList.get(i).getQuestionContent() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("typeId", subjectList.get(i).getCateId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("typeName", subjectList.get(i).getCateName() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("testId", subjectList.get(i).getQuestionId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("skillID", subjectList.get(i).getSkillId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("skillName", subjectList.get(i).getSkillName() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("difficuty", subjectList.get(i).getDifficulty() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("hits", subjectList.get(i).getHits() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("question_id", subjectList.get(i).getQuestionId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("image_src", subjectList.get(i).getImageSrc() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("label", subjectList.get(i).getLabel() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("status", subjectList.get(i).getStatus() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("create_user_id", subjectList.get(i).getCreateUserId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("create_time", subjectList.get(i).getCreateTime() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					
					writer.addDocument(doc);
				}

			} else {
				if (searchCondition.difficuty[0] < subjectList.get(i).getDifficulty()
						&& searchCondition.difficuty[1] > subjectList.get(i).getDifficulty()
						&& searchCondition.skillList.contains(subjectList.get(i).getSkillName())
						&& searchCondition.type == subjectList.get(i).getCateId()) {

					doc.add(new Field("content", subjectList.get(i).getQuestionContent() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("typeId", subjectList.get(i).getCateId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("typeName", subjectList.get(i).getCateName() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("testId", subjectList.get(i).getQuestionId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("skillID", subjectList.get(i).getSkillId() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("skillName", subjectList.get(i).getSkillName() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("difficuty", subjectList.get(i).getDifficulty() + "", Field.Store.YES,
							Field.Index.ANALYZED));
					doc.add(new Field("hits", subjectList.get(i).getHits() + "", Field.Store.YES,
							Field.Index.ANALYZED));

					writer.addDocument(doc);

				}
			}
		}
		writer.close();

		return subjectDirectoryBySearchCondition;
	}

	private static void setUpWordDirectory() throws Exception {
		IndexWriter writer = getCheckWriter();
		List<Skill> skillList = SearchDAOImpl.getSkillList();
		for (int i = 0; i < skillList.size(); i++) { // 3
			Document doc = new Document();
			doc.add(new Field("skillId", skillList.get(i).getSkillId() + "", Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("skillName", skillList.get(i).getSkillName(), Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("fatherSkillId", skillList.get(i).getFatherId() + "", Field.Store.YES,
					Field.Index.ANALYZED));
			writer.addDocument(doc);
		}
		writer.close();
	}

	private static void setUpSubjectDirectory() throws Exception {
		IndexWriter writer = getSubjectWriter();
		List<Question> subjectList = SearchDAOImpl.getSubjectList();
		for (int i = 0; i < subjectList.size(); i++) { // 3
			Document doc = new Document();
			doc.add(new Field("content", subjectList.get(i).getQuestionContent() + "", Field.Store.YES,
					Field.Index.ANALYZED));
			doc.add(new Field("typeId", subjectList.get(i).getCateId() + "", Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("typeName", subjectList.get(i).getCateName() + "", Field.Store.YES,
					Field.Index.ANALYZED));
			doc.add(new Field("testId", subjectList.get(i).getQuestionId() + "", Field.Store.YES,
					Field.Index.ANALYZED));
			doc.add(new Field("skillID", subjectList.get(i).getSkillId() + "", Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("skillName", subjectList.get(i).getSkillName() + "", Field.Store.YES,
					Field.Index.ANALYZED));
			doc.add(new Field("difficuty", subjectList.get(i).getDifficulty() + "", Field.Store.YES,
					Field.Index.ANALYZED));
			doc.add(new Field("hits", subjectList.get(i).getHits() + "", Field.Store.YES, Field.Index.ANALYZED));
			writer.addDocument(doc);
		}
		writer.close();
	}

	private static IndexWriter getCheckWriter() throws IOException { // 2
		return new IndexWriter(skillDirectory, new StandardAnalyzer(Version.LUCENE_30), // 2
				IndexWriter.MaxFieldLength.UNLIMITED); // 2
	}

	private static IndexWriter getSubjectWriter() throws IOException {
		return new IndexWriter(subjectDirectory, new SmartChineseAnalyzer(Version.LUCENE_34), // 2
				IndexWriter.MaxFieldLength.UNLIMITED);
	}
}
